package jmail.sentUtils;


import jmail.entiy.MimeMess;
import jmail.pro.EnvPro;

import javax.mail.*;

import java.util.Properties;

public class SendText{

    private EnvPro envPro;
    private Properties properties;
    private Session session;


    public SendText() {
        this.envPro = new EnvPro();
    }
    public void setProperties(){
        this.properties = new Properties();
        //开启用户认证
        properties.setProperty(envPro.AUTH_METHOD, "true");
        //设置传输协议
        properties.setProperty(envPro.TRANS_PRO, "smtp");
        //设置发件人的SMTP服务器地址
        properties.setProperty(envPro.SMTP_HOST,envPro.SMTP_WANGYI_HOST);
    }
    //默认配置文件
     public void setSession() throws Exception {
        this.setSession(properties);
    }
    //自定义配置文件
    public void setSession(Properties properties) throws Exception {
        if(properties!=null)
        {
            session = Session.getInstance(properties);
            session.setDebug(true);
        }
        else{
            throw new Exception("配置文件为空");
        }
    }

    public Message getMessage() throws MessagingException {
        return new MimeMess(session,"lht","sss").getMessage();
    }

    public void send() throws Exception {
        //获取设置
        Message message = this.getMessage();
        Transport transport = session.getTransport();
        transport.connect(envPro.SENDER_ACC, envPro.SENDER_PSW);
        transport.sendMessage(message,message.getAllRecipients());
        transport.close();
    }


    public static void main(String[] args) throws Exception {
        SendText text = new SendText();
        text.setProperties();
        text.setSession();
        text.send();
    }

}
